const Koa = require('koa')
const app = new Koa()
const router = require('./routes')
const views = require('koa-views')
const serve = require('koa-static')

// 挂载静态页面并载入ejs
app.use(views(__dirname + '/views', {
  map: {
    html: 'ejs'
  }
}))

// 访问静态资源
app.use(serve(__dirname + '/static'))

// 手机端 电脑端
function isMobile(userAgent) {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
}

// 中间件
app.use(async (ctx, next) => {
  const source = ctx.query.source || '';
  const userAgent = ctx.request.header['user-agent'] || '';
  // const host = 'http://192.168.0.112:3000';
  const host = `https://${ctx.request.header.host}`;
  ctx.config = {
    host,
    version: '1.0.1',
    title: '八益驾校',
    description: '八益驾校',
    keyword: '八益驾校',
    source,
    nav: [{
      id: 1,
      title: '网站首页',
      href: `${host}/`,
      checked: false
    }, {
      id: 2,
      title: '公司介绍',
      href: `${host}/gsjs/`,
      checked: false
    }, {
      id: 3,
      title: '考场设施',
      href: `${host}/kcss/`,
      checked: false
    }, {
      id: 4,
      title: '荣誉资质',
      href: `${host}/ryzz/`,
      checked: false
    }, {
      id: 5,
      title: '新闻动态',
      href: '',
      checked: false,
      children: [{
        id: 1,
        title: '企业新闻',
        href: `${host}/qyxw/`
      }, {
        id: 2,
        title: '行业动态',
        href: `${host}/hydt/`
      }]
    }, {
      id: 6,
      title: '校车路线',
      href: `${host}/xclx/`,
      checked: false
    }, {
      id: 7,
      title: '联系我们',
      href: `${host}/lxwm/`,
      checked: false
    }],
    // 荣誉资质
    honor: [{
      id: 1,
      title: '诚信文明企业',
      src: `${host}/imgs/honor1.jpg`
    }, {
      id: 2,
      title: '爱心企业',
      src: `${host}/imgs/honor2.jpg`
    }, {
      id: 3,
      title: '先进基层党组织',
      src: `${host}/imgs/honor3.jpg`
    }, {
      id: 4,
      title: '第六届四川质量论坛理事单位',
      src: `${host}/imgs/honor4.jpg`
    }, {
      id: 5,
      title: '创业云星',
      src: `${host}/imgs/honor5.jpg`
    }, {
      id: 6,
      title: '拥军优属先进单位',
      src: `${host}/imgs/honor6.jpg`
    }, {
      id: 7,
      title: '守合同重信用企业',
      src: `${host}/imgs/honor7.jpg`
    }, {
      id: 8,
      title: '劳动用工诚信单位',
      src: `${host}/imgs/honor8.jpg`
    }],
    navMobile: [{
      id: 1,
      title: '网站首页',
      href: `${host}/`,
      src: `${host}/imgs/nav/nav1.png`
    }, {
      id: 2,
      title: '公司介绍',
      href: `${host}/gsjs/`,
      src: `${host}/imgs/nav/nav2.png`
    }, {
      id: 4,
      title: '考场设施',
      href: `${host}/kcss/`,
      src: `${host}/imgs/nav/nav4.png`
    }, {
      id: 5,
      title: '荣誉资质',
      href: `${host}/ryzz/`,
      src: `${host}/imgs/nav/nav5.png`
    }, {
      id: 6,
      title: '企业新闻',
      href: `${host}/qyxw/`,
      src: `${host}/imgs/nav/nav6.png`
    }, {
      id: 7,
      title: '行业动态',
      href: `${host}/hydt/`,
      src: `${host}/imgs/nav/nav7.png`
    }, {
      id: 8,
      title: '校车路线',
      href: `${host}/xclx/`,
      src: `${host}/imgs/nav/nav8.png`
    }, {
      id: 9,
      title: '联系我们',
      href: `${host}/lxwm/`,
      src: `${host}/imgs/nav/nav9.png`
    }],
    url: ctx.url,
    isMobile: isMobile(userAgent)
  }

  await next()
  // 404
  if (ctx.response.status == 404) {
    ctx.redirect('/404.html')
  }
})

// 挂载路由
app.use(router.routes()).use(router.allowedMethods())

// 配置服务
app.listen(3000, () => {
  console.log('server is running http://localhost:3000')
})